package com.xiaoyu.serviceImpl;

import com.xiaoyu.AccountDao;
import com.xiaoyu.PostDao;
import com.xiaoyu.client.Account_PostDao;
import com.xiaoyu.model.Account;
import com.xiaoyu.model.Post;
import com.xiaoyu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    @Autowired
    private Account_PostDao accountPostDao;
    @Autowired
    private PostDao postDao;
    @Override
    public Account login(Account account) {
        List<Account> accountList=accountDao.findAccount(account);
        if(accountList.size()==1 && accountList.get(0).getState()==1)
            return accountList.get(0);
        return null;
    }

    @Override
    public Boolean addAccount(Account account) {
        account.setState(1);
        account.setPassword("123456");
        if (accountDao.addAccount(account)==1)
            return true;
        return false;
    }

    @Override
    public Boolean deleteAccount(Account account) {
        accountDao.deleteAccount(account);
        accountPostDao.delete(account.getId());
       if(accountDao.findAccount(account).size()==0)
           return true;
       return false;
    }

    @Override
    public Boolean updateAccount(Account account) {
        if(accountDao.updateAccount(account)==1)
            return true;
        return false;
    }

    @Override
    public List<Account> allAccount() {
        List<Account> accountList=accountDao.findAccount(null);
        for (Account a:accountList) {
            a.setPassword("******");
        }
        return accountList;
    }

    @Override
    public Boolean updateAccountClient(Integer aid,String post) {
        Post p=new Post();
        p.setPname(post);
        p=postDao.findOnePost(p);
        if(accountPostDao.update(aid,p.getId())==1)
            return true;
        return false;
    }
}
